<template>
  <div class="message-container">
    <!-- 表格 -->
    <el-table
      ref="scrollerContainer"
      :data="messageList"
      style="width: 100%"
      border
    >
      <el-table-column
        type="index"
        label="序号"
        width="50"
        align="center"
      ></el-table-column>
      <el-table-column label="头像" width="130" align="center">
        <template slot-scope="scope">
          <el-image
            :src="setDevImg(scope.row.avatar)"
            :style="{ width: '80px', height: '80px' }"
            :preview-src-list="[setDevImg(scope.row.avatar)]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" width="120" align="center">
      </el-table-column>
      <el-table-column prop="content" label="留言内容" header-align="center">
      </el-table-column>
      <el-table-column label="评论日期" align="center" width="200">
        <template slot-scope="scope">
          {{
            `${parseTime(
              scope.row.createDate,
              "{y}-{m}-{d} {h}:{i}:{s} 星期{a}"
            )}`
          }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="80">
        <template slot-scope="scope">
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            size="mini"
            @click="handlerDeleteMessage(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      :page-sizes="[10, 50, 80]"
      :page-size="messagePage.limit"
      @size-change="handlePageSizeChange"
      @current-change="handlePageChange"
      :current-page="messagePage.page"
      layout="prev, pager, next, total, ->, sizes, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { setDevImg, parseTime } from "@/utils";
import { getMessageAPI, deleteMessageAPI, addMessageAPI } from "@/api/message";
export default {
  name: "message",
  data() {
    return {
      messagePage: {
        page: 1,
        limit: 10,
        blogid: -1,
        keyword: "",
      },
      total: 0,
      messageList: [],
    };
  },
  created() {
    this.getMessageList();
  },
  methods: {
    setDevImg,
    parseTime,
    async getMessageList() {
      const res = await getMessageAPI(this.messagePage);
      if (typeof res !== "string") {
        this.total = res.data.total;
        this.messageList = res.data.rows;
      } else {
        const { msg } = JSON.parse(res);
        this.$message.error(msg);
      }
    },
    handlerDeleteMessage(row) {
      this.$confirm(`确定要删除 ${row.nickname} 的评论吗?`, "评论删除", {
        type: "warning",
      })
        .then(() => {
          deleteMessageAPI(row.id).then((res) => {
            if (typeof res !== "string") {
              this.$message.success(`${row.nickname}的评论已删除成功！`);
              this.getMessageList();
            } else {
              const { msg } = JSON.parse(res);
              this.$message.error(msg);
            }
          });
        })
        .catch(() => {});
    },
    handlePageChange(currentPage) {
      this.messagePage.page = currentPage;
      this.getMessageList();
      this.$refs.scrollerContainer.$el.scroll(top);
    },
    handlePageSizeChange(pageSize) {
      this.messagePage.page = 1;
      this.messagePage.limit = pageSize;
      this.getMessageList();
      this.$refs.scrollerContainer.$el.scroll(top);
    },
  },
};
</script>

<style scoped lang="scss">
.message-container {
  height: calc(100vh - 50px);
  overflow: hidden;
  padding: 10px;
}

.el-table {
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
  height: calc(100% - 50px);
}
.el-pagination {
  margin: 10px 0;
}
</style>
